
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ------------------
 * PieChartDemo5.java
 * ------------------
 * (C) Copyright 2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: PieChartDemo5.java,v 1.2 2004/04/26 19:12:00 taqua Exp $
 *
 * Changes
 * -------
 * 31-Mar-2004 : Version 1 (DG);
 *
 */

package ucmconfchecker.UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import ucmconfchecker.conformance.Report;

/**
 * A pie chart with no section labels.
 */
public class PieChartView<T> extends ChartPanel {

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public PieChartView(DefaultPieDataset dataset) {
        super(null);
        update(dataset);
    }
    
    public PieChartView(Report<T> report) {
//        final PieDataset dataset = createDataset();
        super(null);
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Correct events", new Integer(report.getWidthoutConflictCounter()));
        dataset.setValue("Precedence errors", new Integer(report.getPrecedenceConflictCounter()));
//        dataset.setValue("Missing preconditions", new Integer(report.getMissingPreconditionCounter()));
        dataset.setValue("Missing responsibility activations", new Integer(report.getMissingEventCounter()));
        dataset.setValue("Unexpected events", new Integer(report.getUnexpectedEventCount()));
        update(dataset);
    }
    void update(DefaultPieDataset dataset){
        final JFreeChart chart = createChart(dataset);
        
        this.setChart(chart);
    }
   
    
    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return a chart.
     */
    private JFreeChart createChart(final DefaultPieDataset dataset) {
        
        final JFreeChart chart = ChartFactory.createPieChart(
            "Report",  // chart title
            dataset,             // data
            false,               // include legend
            true,
            true
        );

        final PiePlot plot = (PiePlot) chart.getPlot();
//        plot.setInteriorGap(0.0);
//      PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}: {2}"));        
        
        // Specify the colors here 
        Color[] colors = {Color.green, Color.yellow, Color.red, Color.BLACK}; 
        PieRenderer renderer = new PieRenderer(colors); 
        renderer.setColor(plot, dataset); 
        
        
        return chart;
        
    }
    
    
    public static JDialog showDialog(JFrame parent, final String title,DefaultPieDataset dataset) {

        JDialog dialog = new JDialog(parent,"Event timeline");	
        PieChartView pieChartView = new PieChartView(dataset);
        dialog.getContentPane().add(pieChartView, BorderLayout.CENTER);
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setBounds(50, 50, 400, 400);
        dialog.setVisible(true);
        RefineryUtilities.centerFrameOnScreen(dialog);
        return dialog;

    }
    public static JDialog showDialog(JFrame parent, final String title,Report report) {

         JDialog dialog = new JDialog(parent,"Event timeline");	
        PieChartView pieChartView = new PieChartView(report);
        dialog.getContentPane().add(pieChartView, BorderLayout.CENTER);
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setBounds(50, 50, 400, 400);
        dialog.setVisible(true);
        RefineryUtilities.centerFrameOnScreen(dialog);
        return dialog;

    }
    
    public static class PieRenderer 
    { 
        private Color[] color; 
        
        public PieRenderer(Color[] color) 
        { 
            this.color = color; 
        }        
        
        public void setColor(PiePlot plot, DefaultPieDataset dataset) 
        { 
            List <Comparable> keys = dataset.getKeys(); 
            int aInt; 
            
            for (int i = 0; i < keys.size(); i++) 
            { 
                aInt = i % this.color.length; 
                plot.setSectionPaint(keys.get(i), this.color[aInt]); 
            } 
        } 
    } 
}

